//
//  SceneryModel.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/15.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "SceneryModel.h"

#define HTTPURL @"http://apis.baidu.com/qunartravel/travellist/travellist"
#define API_KEY @"4fc0970fc22f504982c1fab95ab9c539"

@implementation SceneryModel

- (void)loadNoteListWithCity:(NSString *)city {
    NSString *httpArg = city;
    NSString *urlString = [NSString stringWithFormat:@"%@?query=%@&page=1", HTTPURL, httpArg];
    NSString *urlUTF8 = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSURL *url = [NSURL URLWithString: urlUTF8];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod:@"GET"];
    [request addValue:API_KEY forHTTPHeaderField:@"apikey"];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error.localizedDescription);
        } else {
            id responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.noteList = responseObject[@"data"][@"books"];
            });
        }
    }];
    [dataTask resume];
}

@end
